# -*- coding: UTF8 -*-
import re
import urllib
import md5
from sgmllib import SGMLParser
from collections import deque
from urllib import unquote

p = re.compile('^/wiki/')
p1 = re.compile('^/wiki/Category:')
pEx = re.compile('.*[:#].*')

class URLListener(SGMLParser):
	def reset(self):
		SGMLParser.reset(self)
		self.urls = []

	def start_a(self, attrs):
		href = [v for k, v in attrs if k == 'href']
		if href:
			self.urls.extend(href)

mypath = r'http://zh.wikipedia.org'
q = deque(['/wiki/算法','/wiki/数据结构','/wiki/编译器','/wiki/计算机','/wiki/图灵机'])
visited = set(['/wiki/算法','/wiki/数据结构','/wiki/编译器','/wiki/计算机','/wiki/图灵机'])

def search(name):
	urls = set([name])
	url = mypath+name
	sock = urllib.urlopen(url)
	htmlSource = sock.read()
	sock.close()
	f = open(name[6:], 'w')
	parser = URLListener()
	parser.feed(htmlSource)
	for urlx in parser.urls:
		if ((not pEx.match(urlx)) and p.match(urlx)) or p1.match(urlx):
			urlx = unquote(urlx)
			if urlx not in urls:
				urls.add(urlx)
				myurl = mypath+urlx+'\n'
				f.write(myurl)
				if urlx not in visited:
					q.append(urlx)
					visited.add(urlx)
	f.close()

while len(q) > 0:
	name = q.popleft()
	search(name)
